package com.acme.entity;

import com.acme.entity.util.ChangeSupportList;
import com.acme.entity.util.EntityBean;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.swing.event.ChangeListener;

/**
 *
 * @author alejandro
 */
@Entity
@Table(name = "COMBUSTIBLE", catalog = "carreteras", schema = "dbo")
@NamedQueries({
    @NamedQuery(name = "Combustible.findAll", query = "SELECT c FROM Combustible c"),
    @NamedQuery(name = "Combustible.findById", query = "SELECT c FROM Combustible c WHERE c.id = :id"),
    @NamedQuery(name = "Combustible.findByDescripcion", query = "SELECT c FROM Combustible c WHERE c.descripcion = :descripcion")})
public class Combustible extends EntityBean<Combustible> {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "descripcion", nullable = false, length = 30)
    private String descripcion;
    @JoinTable(name = "GASOLINERIA_COMBUSTIBLE", joinColumns = {
        @JoinColumn(name = "combustible", referencedColumnName = "id", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(name = "gasolineria", referencedColumnName = "id", nullable = false)})
    @ManyToMany
    private final ChangeSupportList<Gasolineria> gasolineriaList = new ChangeSupportList<Gasolineria>();

    public Combustible() {
    }

    public Combustible(Integer id) {
        this.id = id;
    }

    public Combustible(Integer id, String descripcion) {
        this.id = id;
        this.descripcion = descripcion;
    }

    public Combustible(Integer id, String descripcion, List<Gasolineria> gasolineriaList) {
        this.id = id;
        this.descripcion = descripcion;
        mergeListContent(this.gasolineriaList, gasolineriaList);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
        support.fireChange();
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
        support.fireChange();
    }

    public List<Gasolineria> getGasolineriaList() {
        return gasolineriaList;
    }

    public void setGasolineriaList(List<Gasolineria> gasolineriaList) {
        mergeListContent(this.gasolineriaList, gasolineriaList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Combustible)) return false;
        Combustible other = (Combustible) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return descripcion;
    }

    @Override
    public void addChangeListener(ChangeListener listener) {
        gasolineriaList.addChangeListener(listener);
        super.addChangeListener(listener);
    }

    @Override
    public Combustible clone() {
        return new Combustible(id, descripcion, gasolineriaList);
    }

    @Override
    public void merge(Combustible other) {
        this.id = other.id;
        this.descripcion = other.descripcion;
        mergeListContent(this.gasolineriaList, other.gasolineriaList);
    }

}
